package org.raymond.iworks.study.example;

import cn.hutool.core.util.HexUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.raymond.iworks.apis.vo.user.UserPageVO;
import org.raymond.iworks.commons.utils.RandomValueUtil;
import org.raymond.iworks.repository.entity.TSysUser;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

@Slf4j
@SpringBootTest
public class ExcelTest {

    public List<TSysUser> list(){
        String pwd = "123456";
        String countryCode = "+86";
        String start = "1980-01-01 00:00:00";
        String end = "2000-01-01 00:00:00";

        TSysUser liyuanqiong = TSysUser.builder().username("liyuanqiong").password(pwd).countryCode(countryCode).birthday(RandomValueUtil.getDate(start, end))
                .mobile(RandomValueUtil.getTel()).email(RandomValueUtil.getEmail(8,12)).build();
        TSysUser wangle = TSysUser.builder().username("wangle").password(pwd).countryCode(countryCode).birthday(RandomValueUtil.getDate(start, end))
                .mobile(RandomValueUtil.getTel()).email(RandomValueUtil.getEmail(8,12)).build();
        TSysUser zhaomaoping = TSysUser.builder().username("zhaomaoping").password(pwd).countryCode(countryCode).birthday(RandomValueUtil.getDate(start, end))
                .mobile(RandomValueUtil.getTel()).email(RandomValueUtil.getEmail(8,12)).build();
        TSysUser zhouyang = TSysUser.builder().username("zhouyang").password(pwd).countryCode(countryCode).birthday(RandomValueUtil.getDate(start, end))
                .mobile(RandomValueUtil.getTel()).email(RandomValueUtil.getEmail(8,12)).build();

        List<TSysUser> users = Lists.newArrayList(liyuanqiong, wangle, zhaomaoping, zhouyang);
        return users;
    }

    @Test
    public void test02(){
        //List<TSysUser> data = list();
        //EasyExcel.write("../docs/excel/downloads/a.xlsx", String.class).sheet("1").doWrite(data);

        File file = new File("../docs/excel/downloads/user.xlsx");
        try{
            InputStream oriIs = new FileInputStream(file);
            byte[] oriBytes = oriIs.readAllBytes();
            /*
            String oriStr = new String(oriBytes, StandardCharsets.UTF_8);
            String hexStr =HexUtil.encodeHexStr(oriStr, StandardCharsets.UTF_8);
            String base64Str = Base64.encode(hexStr, StandardCharsets.UTF_8);
            byte[] decoded = Base64.decode(base64Str);
            String content = StrUtil.str(decoded, StandardCharsets.UTF_8);
            log.info("content:{}", content);
             */
            //byte[] bytes = StrUtil.bytes(oriStr, StandardCharsets.UTF_8);

            //System.out.println("s:"+s+"\nr:"+r+"\nr2:"+r2);
            //byte[] sd2 = r2.getBytes(StandardCharsets.UTF_8);
            String oriStr =HexUtil.encodeHexStr(oriBytes);
            byte[] bytes = HexUtil.decodeHex(oriStr);
            InputStream is2 = new ByteArrayInputStream(bytes);
            EasyExcel.read(is2, UserPageVO.class, new PageReadListener<UserPageVO>(list->{
                for(UserPageVO user: list){
                    System.out.println("user: "+ JSONUtil.toJsonStr(user));
               }
            }
            )).excelType(ExcelTypeEnum.XLSX).sheet().doRead();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
